from django.http import JsonResponse
from django.db.models import F
from django.shortcuts import render
from app01 import models
import pandas as pd


def chart_list(request):
    return render(request, "chart_list.html")


def chart_bar(request):
    bar_set = models.UserInfo.objects.values().order_by('create_time')
    df_bar = pd.DataFrame(bar_set)

    data_dict = {
        'account': df_bar['account'].values.tolist(),
        'create_time': df_bar['create_time'].values.tolist()
    }
    # print(bar_set)
    return JsonResponse({"status": True, 'data': data_dict})


def chart_pie(request):
    pie_set = models.Order.objects.values(value=F('price'), name=F('title'))
    # print(pie_set)
    return JsonResponse({"status": True, 'data': list(pie_set)})


def chart_line(request):
    line_set = models.UserInfo.objects.values('account', 'create_time').order_by('create_time')

    data_dict = {
        'account': [user['account'] for user in line_set],
        'create_time': [user['create_time'] for user in line_set]
    }
    print(data_dict)
    return JsonResponse({"status": True, 'data': data_dict})
